﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
namespace GUI
{
    public partial class frmPhanMemQuanLy : Form
    {
        public frmPhanMemQuanLy()
        {
            InitializeComponent();
        }
        private bool CheckExistForm(string name)
        {
            foreach (Form frm in this.MdiChildren)
                if (frm.Name == name)
                    return true;
            return false;
        }
        private void ActiveChildForm(string name)
        {
            foreach (Form frm in this.MdiChildren)
                if (frm.Name == name)
                {
                    frm.Activate();
                    break;
                }
        }
        private void mnDangNhap_Click(object sender, EventArgs e)
        {
            frmDangNhap frm = new frmDangNhap();
            frm.ShowDialog();
        }

        private void mnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void mnDangXuat_Click(object sender, EventArgs e)
        {

        }

        private void mnThemMon_Click(object sender, EventArgs e)
        {
            panel_chinh.Hide();
        }

        private void mnDatMon_Click(object sender, EventArgs e)
        {
            panel_chinh.Hide();
            if (!CheckExistForm("frmDatMon"))
            {
                frmDatMon frm = new frmDatMon();
                frm.MdiParent = this;
                frm.Height = int.Parse(this.Height.ToString())-70;
                frm.Width = int.Parse(this.Width.ToString())-20;
                frm.Show();
            }
            else
                ActiveChildForm("frmDatMon");
        }

        private void mnChiTietDatMon_Click(object sender, EventArgs e)
        {
            panel_chinh.Hide();
            if (!CheckExistForm("frmChiTietDatMon"))
            {
                frmChiTietDatMon frm = new frmChiTietDatMon();
                frm.MdiParent = this;
                frm.Height = int.Parse(this.Height.ToString()) - 70;
                frm.Width = int.Parse(this.Width.ToString()) - 20;
                frm.Show();
            }
            else
                ActiveChildForm("frmChiTietDatMon");
            
        }

        private void mnQuanLyDatMon_Click(object sender, EventArgs e)
        {
            panel_chinh.Hide();
            if (!CheckExistForm("frmQuanLyDatMon"))
            {
                frmQuanLyDatMon frm = new frmQuanLyDatMon();
                frm.MdiParent = this;
                frm.Height = int.Parse(this.Height.ToString()) - 70;
                frm.Width = int.Parse(this.Width.ToString()) - 20;
                frm.Show();
            }
            else
                ActiveChildForm("frmQuanLyDatMon");
        }

        private void mnQuanLyGiaoHang_Click(object sender, EventArgs e)
        {
            panel_chinh.Hide();
            if (!CheckExistForm("frmQuanLyDatMon"))
            {
                frmQuanLyNauCom frm = new frmQuanLyNauCom();
                frm.MdiParent = this;
                frm.Show();
            }
            else
                ActiveChildForm("frmQuanLyNauCom");
        }

        private void mnQuanLyGiaoCom_Click(object sender, EventArgs e)
        {
            panel_chinh.Hide();
            if (!CheckExistForm("frmQuanLyDatMon"))
            {
                frmQuanLyGiaoCom frm = new frmQuanLyGiaoCom();
                frm.MdiParent = this;
                frm.Show();
            }
            else
                ActiveChildForm("frmQuanLyNauCom");
        }

        private void mnQuanLyThuHop_Click(object sender, EventArgs e)
        {
            panel_chinh.Hide();
            if (!CheckExistForm("frmQuanLyThuHop"))
            {
                frmQuanLyThuHop frm = new frmQuanLyThuHop();
                frm.MdiParent = this;
                frm.Show();
            }
            else
                ActiveChildForm("frmQuanLyThuHop");
        }
        

        private void frmPhanMemQuanLy_Load(object sender, EventArgs e)
        {
            int ngay = System.DateTime.Today.Day;
            int thang = System.DateTime.Today.Month;
            int nam = System.DateTime.Today.Year;
            lbNgay.Text = ngay + "/"+ thang +"/" + nam;
          
            HeThongLenlich(ngay, thang, nam);

            DanhSachMonAn(ngay, thang, nam);
        }

        private void lịchGiaoCơmToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panel_chinh.Hide();
            if (!CheckExistForm("frmLichGiao"))
            {
                frmLichGiao frm = new frmLichGiao();
                frm.MdiParent = this;
                frm.Show();
            }
        }

        private void mnXemHop_Click(object sender, EventArgs e)
        {
            panel_chinh.Hide();
            if (!CheckExistForm("frmXemHop"))
            {
                frmXemHop frm = new frmXemHop();
                frm.MdiParent = this;
                frm.Height = int.Parse(this.Height.ToString()) - 70;
                frm.Width = int.Parse(this.Width.ToString()) - 20;
                frm.Show();
            }
            else
                ActiveChildForm("frmQuanLyDatMon");
           
        }

        private void DanhSachMonAn(int ngay,int thang,int nam)
        {
            
            LichHTBUS busHT= new LichHTBUS();
            List<LichHTDTO> dsHT = busHT.DanhSachTheoThang(thang,nam);
            for (int i = 0; i < dsHT.Count;i++)
            {
                MonAnBUS busMA = new MonAnBUS();
                MonAnDTO ma=busMA.LayThongTinMonAn( dsHT[i].MaMA);
                string tenma = ma.TenMA;
               
                LoaiMonAnBUS busLMA = new LoaiMonAnBUS();
                LoaiMonAnDTO lma = busLMA.LayThongTinLoaiMonAn(ma.LoaiMA);
                double dongia = lma.DonGia;
                dgvDanhSach.Rows.Add(i + 1,tenma,dongia);
            }
        }
        private void HeThongLenlich(int ngay, int thang, int nam)
        {
            //LichHTBUS HTBus = new LichHTBUS();
            //int sl=HTBus.MaChaMay(thang, nam);
            //MessageBox.Show(sl.ToString());


            LoaiMonAnBUS busLMA = new LoaiMonAnBUS();
            List<LoaiMonAnDTO> dslma= new List<LoaiMonAnDTO>() ;
            dslma = busLMA.DanhSachLoaiMonAn(); //danh sách LOAIMONAN
            for (int i = 0; i < dslma.Count; i++)
            {
                MonAnBUS busMA = new MonAnBUS();
                List<MonAnDTO> dsma = new List<MonAnDTO>();
                dsma = busMA.DanhSachMonAn(dslma[i].MaLoaiMA); //danh sách MONAN theo "mã loại" món ăn
                for (int j = 0; j < dsma.Count; j++)
                {
                    // kiểm tra với @MaLoaiMA có tồn tại MaMA trong LICHHT của tháng đó chưa.
                    //  1: đã tồn tại
                    // -1: chưa tồn tại
                    string MaMA = dsma[j].MaMA;
                    LichHTBUS busHT = new LichHTBUS();
                    int res = busHT.MonAnChuaLenLich(MaMA, thang, nam);                   
                    string date = thang + "/" + ngay + "/" + nam;
                    if (res == -1)
                    {                         
                         LichHTBUS busHTT = new LichHTBUS(); 
                         busHTT.ThemMonAnHT(MaMA, date);
                    }
               }
                
            }
        }

        private void mnXemDonhang_Click(object sender, EventArgs e)
        {
            panel_chinh.Hide();
            if (!CheckExistForm("frmXemDonHang"))
            {
                frmXemDonHang frm = new frmXemDonHang();
                frm.MdiParent = this;
                frm.Height = int.Parse(this.Height.ToString()) - 70;
                frm.Width = int.Parse(this.Width.ToString()) - 20;
                frm.Show();
            }
            else
                ActiveChildForm("frmXemDonHang");
        }

        private void hủyĐơnHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panel_chinh.Hide();
            if (!CheckExistForm("frmHuyDonHang"))
            {
                frmHuyDonHang frm = new frmHuyDonHang();
                frm.MdiParent = this;
                frm.Show();
            }
        }


    }
}
